const config = require('./config')

class Request {
	request(url, data, method) {
		uni.showLoading({
			title: "正在加载中..."
		})
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.baseUrl + url,
				data,
				method,
				success(res) {
					switch (res.statusCode) {
						case 200:
							resolve(res.data)
							uni.hideLoading();
							break;
						case 401:
							uni.hideLoading();
							uni.showToast({
								title: '未授权',
								icon: 'error',
								duration: 2000
							});
							break;
						case 404:
							uni.hideLoading();
							uni.showToast({
								title: 'URL未找到',
								icon: 'error',
								duration: 2000
							});
							break;
						case 500:
							uni.hideLoading();
							uni.showToast({
								title: '服务器错误',
								icon: 'error',
								duration: 2000
							});
							break;
					}
				}
			})
		})
	}

	get({
		url,
		data
	}) {
		return this.request(url, data, 'GET')
	}
}

export default new Request()
